import requests
import bs4
import os
import shutil
from threading import Thread
from time import sleep

nowNum = 0


def open_url(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
    }
    res = requests.get(url, headers=headers)
    return res


def savePic(url, num, picFormat):
    global nowNum
    nowNum = nowNum + 1
    if os.path.exists("图片\\" + str(num).rjust(3,'0') + "." + picFormat) == True:
        return True
    print(f"正在处理第{num}张图片：")
    res = open_url(url.format(num))
    if ("HTML" in res.text or "html" in res.text):
        print("失败")
        return False
    try:
        f = open("图片\\" + str(num) + "." + picFormat, "wb")
        f.write(res.content)
        f.close()
        print("成功")
        return True
    except Exception:
        print("失败")
        return False


def main():
    url = input("请输入图片链接地址")
    # url = ""
    picFormat = url[-3:]
    if os.path.exists("图片\\") == False:
        os.makedirs("图片\\", exist_ok=True)
    else:
        shutil.rmtree("图片\\")
        os.makedirs("图片\\", exist_ok=True)
    error = 0
    for n in range(500):
        Thread1 = Thread(target=savePic, args=(url, nowNum, picFormat))
        Thread2 = Thread(target=savePic, args=(url, nowNum + 1, picFormat))
        Thread3 = Thread(target=savePic, args=(url, nowNum + 2, picFormat))
        Thread4 = Thread(target=savePic, args=(url, nowNum + 3, picFormat))
        Thread5 = Thread(target=savePic, args=(url, nowNum + 4, picFormat))
        Thread6 = Thread(target=savePic, args=(url, nowNum + 5, picFormat))
        Thread7 = Thread(target=savePic, args=(url, nowNum + 6, picFormat))
        Thread8 = Thread(target=savePic, args=(url, nowNum + 7, picFormat))
        Thread9 = Thread(target=savePic, args=(url, nowNum + 8, picFormat))
        Thread10 = Thread(target=savePic, args=(url, nowNum + 9, picFormat))

        Thread1.start()
        Thread2.start()
        Thread3.start()
        Thread4.start()
        Thread5.start()
        Thread6.start()
        Thread7.start()
        Thread8.start()
        Thread9.start()
        Thread10.start()

        Thread1.join()
        Thread2.join()
        Thread3.join()
        Thread4.join()
        Thread5.join()
        Thread6.join()
        Thread7.join()
        Thread8.join()
        Thread9.join()
        Thread10.join()


"""         print("正在处理第{}张图片：".format(n + 1))
        try:
            res = open_url(url.format(n + 1))
            # res = open_url(url.format(str(n + 1).zfill(3)))
            if ("HTML" in res.text or "html" in res.text):
                print("失败")
                error = error + 1
                if error >= 10:
                    print("10次请求数据失败，即将结束程序")
                    print(res.text)
                    os._exit(0)
                continue
            with open("图片\\" + str(n+1) + "." + picFormat, "wb") as f:
                f.write(res.content)
                f.close()
                print("成功")
                error = 0
        except Exception as e:
            print("失败")
            error = error + 1
            if error >= 10:
                print("10次请求数据失败，即将结束程序")
                print(e)
                os._exit(0)
            continue """

if __name__ == '__main__':
    main()
